/*
 * Copyright (C) 2009 Ionut Dediu <deionut@yahoo.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// CommFilter.h

#ifndef __COMM_FILTER_H__
#define __COMM_FILTER_H__

#include <e32base.h>			// CActive
#include <es_sock.h>
#include <bt_sock.h>			// TBTSockAddr
#include <in_sock.h>			// TInetAddr

#include "SmartStream.h"		// MSmartFilter, MPipelineMgr
#include "BtDiscoverer.h"	// CSmartCamDiscoverer
#include "TimeWrap.h"			// CTimeWrap, MTimeWrapObserver
#include "SmartCamConst.h"

typedef enum
	{
	EPacketTypeJPEGHeader = 0,
	EPacketTypeJPEGData = 1
	} TSmartCamPacketType;

class CCommFilter : public CActive, public MStreamFilter, public MBtDiscoveryObserver, public MTimeWrapObserver
{
public:

	static CCommFilter* NewL();
    static CCommFilter* NewLC();
    ~CCommFilter();

	void Open(MStreamManager* aMgr);
	void Close();
	TBool CanAcceptMore();
	void SendPacket(TSmartCamPacketType aPacketType, const TDesC8& aPacketData);

	// From MBtDiscoveryObserver:
	void RemoteDeviceSelected(TInt aErrorCode);
	void RemoteServiceFound(TInt aErrorCode);

	// From MTimeWrapObserver:
	void TimeElapsed();

protected:

	// From CActive:
    void DoCancel();
    void RunL();

private:

	CCommFilter();
    void ConstructL();

	// New methods:
	void ConnectToBtServerL();
	void ConnectToInetServerNameL(const TDesC& aInetServerName);
	void ConnectToInetServerAddrL(TUint32 aInetServerAddr);
	void StartBtDiscovery();
	void AssureSizeL(HBufC8*& aBuffer, TInt aSize);

	// Member variables
	// Filter data:
	MStreamManager* iStreamMgr;
	// Comm data:
	RSocketServ iSocketServer;
	RSocket iClientSocket;
	TBTSockAddr iBtServerAddr;
    TInetAddr iInetServerAddr;

    // DNS name resolver.
    RHostResolver iResolver;
    // the result from the name resolver.
    TNameEntry iNameEntry;
    // the name record found by the resolver.
    TNameRecord iNameRecord;

    TBTDevAddr iBtDevAddr;
	CBtDiscoverer* iBtDiscoverer;

	CTimeWrap* iTimeWrap;

	HBufC8* iSendBuffer1;
	HBufC8* iSendBuffer2;
	HBufC8* iCrtSendBuffer;

	enum TCommState
		{
		EDisconnected = 0,
		EDiscovering = 1,		// Used for both bluetooth discovering and DNS lookup
		EConnecting = 2,
		EConnected = 3,
		ESendingPacket = 4,
		ESentPacket = 5,
		} iCommState;
};

#endif//__COMM_FILTER_H__
